package it.polimi.traveldream.web.beans;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import it.polimi.traveldream.ejb.management.EscursioneMgr;
import it.polimi.traveldream.ejb.management.HotelMgr;
import it.polimi.traveldream.ejb.management.PackMng;
import it.polimi.traveldream.ejb.management.PackDTO;
import it.polimi.traveldream.ejb.management.VoloMgr;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.swing.JOptionPane;

@ManagedBean(name = "pacchettoBean")
@RequestScoped
public class PacchettiBean {

	private PackDTO selectedPack;
	
	private PackDTO pacchetto = new PackDTO();
	
	private List<PackDTO> listaPacchetti = new ArrayList<PackDTO>();
	
	@EJB
	private HotelMgr hotelMgr;
	
	@EJB
	private EscursioneMgr escMgr;
	
	@EJB
	private VoloMgr voloMgr;
	
	@PostConstruct
	private void init() {
		listaPacchetti = packMgr.getListaPacchetti();
	}
	
	@EJB
	private PackMng packMgr;
	
	public void setName(String nome) {
		packMgr.getPackDTO().setNome(nome);
	}
	
	public List<PackDTO> getAllPacchetti(){
		listaPacchetti = packMgr.getListaPacchetti();
		return getPacchetti();
		
	}
	
	public List<PackDTO> getDisponibili() {
		listaPacchetti = packMgr.listaPaccDisp();
		return getPacchetti();
	}
	
	public List<PackDTO> getPacchetti() {
		return listaPacchetti;
	}
	
	public PackDTO getSelectedPack() {  
        return selectedPack;  
    }
	
	public void setSelectPack(PackDTO selectedPack) {
		this.selectedPack = selectedPack;
	}
	

    public String cancella(){
		packMgr.delete(selectedPack);
		return "/pagine_impiegati/index?faces-redirect=true";
	}

	public PackDTO getPacchetto() {
		return pacchetto;
	}
	
	public void setPacchetto(PackDTO pacchetto) {
		this.pacchetto = pacchetto;
	}
	
	public String aggiungi() {
		if(pacchetto.getDataArrivo().compareTo(pacchetto.getDataPartenza()) <= 0 ||
			!(hotelMgr.cercaById(pacchetto.getHotel()).getCitta().equals(pacchetto.getLuogo())) ||
			!(escMgr.getEscById(pacchetto.getEscursione()).get(0).getCitta().equals(pacchetto.getLuogo()))){
			return "/pagine_impiegati/erroreCreazione?faces-redirect=true";
		}
		else{
		packMgr.save(pacchetto);
		return "/pagine_impiegati/pacchettoOk?faces-redirect=true";}
	}
	
	
	private String ric; 

	public String getRic() {
		return ric;
	}

	public void setRic(String ric) {
		this.ric = ric;
	}

	public void ricercaCitta() {
		listaPacchetti =  packMgr.searchCitta(ric);
		
	}
	
	public void ricercaCittaDisp() {
		listaPacchetti = packMgr.searchCittaDisp(ric);
	}
	
	public void ricercaNome() {
		listaPacchetti = packMgr.searchByName(ric);
	}
	
	public String ricercaHome() {
		return "home?faces-redirect=true";
	}
	
	public PackDTO getPackById(int id) {
		return packMgr.searchById(id);
	}
	
	public String confirmDel(){
		cancella();
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cancellazione completata",  "");  

		FacesContext.getCurrentInstance().addMessage(null, message);
		return "/pagine_impiegati/index?faces-redirect=true";

	}
    
}
